package com.ss.android.article.base.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.os.SystemClock;
import android.util.AttributeSet;

import com.bytedance.common.b.UserAgentCompat;
import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.feature.detail.view.DetailScrollView;
import com.ss.android.article.base.feature.detail.view.OnOverScrolledListener;
import com.ss.android.article.base.feature.detail.view.OnScrollBarShowListener;

import java.util.Iterator;
import java.util.LinkedList;

@TargetApi(value = 9)
public class MyWebViewV9 extends ScrollListenerWebView {
    public LinkedList<DeltaYRecorder> mDeltaYRecorderList = new LinkedList();
    private int mLastDeltaY;
    private int mScrollRangeY;
    private int mVerticalScrollRange;
    private OnOverScrolledListener<MyWebViewV9> mOnOverScrolledListener;
    private DetailScrollViewHolder mDetailScrollViewHolder;
    private int mContentHeight;
    private boolean detectContentSize;
    private OnScrollBarShowListener mOnScrollBarShowListener;

    public MyWebViewV9(Context context) {
        super(context);
    }

    public MyWebViewV9(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWebViewV9(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
    }

    public int computeVerticalScrollExtent() {
        return super.computeVerticalScrollExtent();
    }

    public int computeVerticalScrollOffset() {
        return super.computeVerticalScrollOffset();
    }

    public int computeVerticalScrollRange() {
        this.mVerticalScrollRange = super.computeVerticalScrollRange();
        return this.mVerticalScrollRange;
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (this.detectContentSize) {
            int contentHeight = this.getContentHeight();
            if (Math.abs(contentHeight - this.mContentHeight) >= 10) {
                int v1 = contentHeight - this.mContentHeight;
                this.mContentHeight = contentHeight;
                if (this.mDetailScrollViewHolder != null) {
                    if (v1 > 0
                            && (mDetailScrollViewHolder.detailScrollView.mEnableDetectContentSizeChange)
                            && mDetailScrollViewHolder.detailScrollView.mMyWebViewV9 != null
                            && mDetailScrollViewHolder.detailScrollView.mMyWebViewV9.getVisibility() == VISIBLE
                            && mDetailScrollViewHolder.detailScrollView.mLayoutType == 1) {
                        int scrollY = mDetailScrollViewHolder.detailScrollView.getScrollY();
                        if (scrollY > 0 && scrollY < mDetailScrollViewHolder.detailScrollView.getWebViewHeight()) {
                            if (v1 < scrollY) {
                                mDetailScrollViewHolder.detailScrollView.mMyWebViewV9.scrollBy(0, v1);
                            } else {
                                mDetailScrollViewHolder.detailScrollView.mMyWebViewV9.scrollBy(0, scrollY);
                            }

                            mDetailScrollViewHolder.detailScrollView.scrollTo(0, 0);
                        }
                        mDetailScrollViewHolder.detailScrollView.mMyWebViewV9.setDetectContentSize(false);
                    }
                }
            }
        }

        if (this.mDetailScrollViewHolder != null) {
            this.getScaledContentHeight();
        }
    }

    public int getComputedVerticalScrollRange() {
        return this.mVerticalScrollRange;
    }

    public int getScaledContentHeight() {
        return ((int) (UserAgentCompat.getScale(this) * (float) this.getContentHeight()));
    }

    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
        } catch (Exception exception) {
        }
    }

    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (this.mOnOverScrolledListener != null) {
            this.mOnOverScrolledListener.onOverScrolled(this, scrollY, clampedY, this.mLastDeltaY, this.mScrollRangeY);
        }
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY,
                                   int scrollX, int scrollY,
                                   int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY,
                                   boolean isTouchEvent) {
        if (this.mOnScrollBarShowListener != null) {
            this.mOnScrollBarShowListener.onScroll();
        }
        this.mLastDeltaY = deltaY;
        this.mScrollRangeY = scrollRangeY;
        int v10 = deltaY <= 0 || scrollY <= 0 ? maxOverScrollY : 0;
        if (this.mDeltaYRecorderList.size() >= 10) {
            this.mDeltaYRecorderList.removeFirst();
        }

        if (deltaY + scrollY < scrollRangeY) {
            this.mDeltaYRecorderList.add(new DeltaYRecorder(deltaY, SystemClock.uptimeMillis()));
            if (Logger.debug()) {
            }
        } else if (!this.mDeltaYRecorderList.isEmpty()) {
            Iterator<DeltaYRecorder> iterator = this.mDeltaYRecorderList.iterator();
            int totalDeltaY;
            for (totalDeltaY = 0; iterator.hasNext(); totalDeltaY = iterator.next().deltaY + totalDeltaY) {
            }
            int duration = (int) (this.mDeltaYRecorderList.getLast().uptimeMillis - this.mDeltaYRecorderList.getFirst().uptimeMillis);
            this.mDeltaYRecorderList.clear();
            if (duration > 0 && totalDeltaY != 0) {
                int v = totalDeltaY / duration * 1000;
                if (this.mOnOverScrolledListener != null && v != 0) {
                    this.mOnOverScrolledListener.fling(v);
                    if (Logger.debug()) {
                        Logger.d("travis", "MyWebViewV9:::overScrollBy: v = " + v);
                    }

                    if (Logger.debug()) {
                        Logger.d("travis", "MyWebViewV9:::overScrollBy:" + deltaY + "/" + scrollY + "/" + scrollRangeY + "/" + SystemClock.uptimeMillis());
                    }
                }
            }
        }
        return super.overScrollBy(deltaX, deltaY,
                scrollX, scrollY,
                scrollRangeX, scrollRangeY,
                maxOverScrollX, v10, isTouchEvent);
    }

    public void setContentSizeChangeListener(DetailScrollViewHolder detailScrollViewHolder) {
        this.mDetailScrollViewHolder = detailScrollViewHolder;
    }

    public void setDetectContentSize(boolean detectContentSize) {
        if (this.detectContentSize != detectContentSize) {
            this.detectContentSize = detectContentSize;
            if (detectContentSize) {
                this.mContentHeight = this.getContentHeight();
            }
        }
    }

    public void setOnOverScrolledListener(OnOverScrolledListener onOverScrolledListener) {
        this.mOnOverScrolledListener = onOverScrolledListener;
    }

    public void setOnScrollBarShowListener(OnScrollBarShowListener onScrollBarShowListener) {
        this.mOnScrollBarShowListener = onScrollBarShowListener;
    }


    public static final class DetailScrollViewHolder {
        private final DetailScrollView detailScrollView;

        public DetailScrollViewHolder(DetailScrollView detailScrollView) {
            this.detailScrollView = detailScrollView;
        }
    }

    final class DeltaYRecorder {
        int deltaY;
        long uptimeMillis;

        public DeltaYRecorder(int deltaY, long uptimeMillis) {
            this.deltaY = deltaY;
            this.uptimeMillis = uptimeMillis;
        }
    }
}

